package com.itheima.demo;

import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

/**/
public class DemoTest_02 {
package com.itheima.demo;

import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

    public class DemoTest1 {
        private SqlSession session =null;
        private InputStream is =null;
        //@Before是在 @Test之前执行的方法
        @Before
        public void before()throws Exception{
            //1、读取核心配置文件
            is = Resources.getResourceAsStream("sql.xml");
            //2、构建SqlSessionFactory对象
            SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
            //3、获取SqlSession对象
            session = factory.openSession();
        }
        //@Before是在 @Test之前执行的方法
        @After
        public void after() throws Exception{
            //5、事务提交
            session.commit();
            //6、释放资源
            session.close();
            is.close();
        }
        //新增操作
        @Test
        public void testSave(){
            //封装user数据
            User user=new User();
            user.setUsername("贾队长");
            user.setSex("男");
            user.setAge(31);
            user.setPassword("123456");
            user.setEmail("666@qq.com");
            System.out.println("执行保存前："+user);
            session.insert("one.saveUser",user);
            System.out.println("执行保存后："+user);
        }
        //模糊查询测试
        @Test
        public void testFindLike(){
            List<User> list = session.selectList("one.findLikeName", "%黄%");
            System.out.println(list);
        }

        //修改测试
        @Test
        public void testFindByEmailAndPwd(){
            //封装数据
            Map map=new HashMap();
            map.put("em","444@qq.com");
            map.put("pd","444");
            User user = session.selectOne("one.findByEmailAndPwd",map);
            System.out.println(user);
        }

        //修改测试
        @Test
        public void testUpdateById(){
            User user = session.selectOne("one.findById", 12);
            user.setUsername("黄金标");
            session.update("one.updateById",user);
        }



        //测试统计男女数量
        @Test
        public void testFindSex(){
            List<Map> map = session.selectList("one.findSex");
            System.out.println(map);
            //重新封装我们所需要的数据
            Map sexMap=new HashMap();
            for (Map map1 : map) {
                String sex = (String) map1.get("sex");
                Long total = (Long) map1.get("count(*)");
                //System.out.println(sex+"--"+total);
                sexMap.put(sex,total);
            }
            System.out.println(sexMap);
        }


        //测试统计人员数量
        @Test
        public void testFindCount(){
            Integer i = session.selectOne("one.findCount");
            System.out.println(i);
        }
        //根据id查询
        @Test
        public  void testFindById() throws Exception{
            //4、调用statement对象并执行:namespace+"."+statement对象的id
            User user = session.selectOne("one.findById", 11);
            System.out.println(user);
        }
        //查询所有
        @Test
        public  void testFindAll() throws Exception{
            //4、调用statement对象并执行:namespace+"."+statement对象的id
            List<User> list = session.selectList("one.findAll");
            System.out.println(list);

        }
    }

}
